
import { _decorator, Component, Label, Node, Animation } from 'cc';
import { MenuItem } from './MenuItem';
import { DataHelper } from '../system/DataHelper';
import { GameData } from '../system/GameData';
import EvtManger from '../tools/EvtManger';
import { AppControll, EventName } from '../manager/AppControll';
import { Tools } from '../tools/Tools';
const { ccclass, property } = _decorator;

@ccclass('MenuView')
export class MenuView extends Component {
    @property(Node)
    mContent: Node = null;
    @property(Label)
    mLbGold: Label = null;
    @property(Label)
    mLbKills: Label = null;
    @property(Label)
    mLbPearls: Label = null;
    @property(Node)
    mNdGuide: Node = null;

    private mBIsGuideLock = true;

    protected onLoad(): void {
        this.InitMenuItem();
        this.RegisteredEvent();
        this.InitLabel();
        this.mNdGuide.getComponent(Animation).on(Animation.EventType.FINISHED, () => {
            this.mBIsGuideLock = false;
        })
    }


    InitLabel() {
        this.mLbGold.string = Tools.FormatNumber(GameData.playerData.Gold);
        this.mLbKills.string = Tools.FormatNumber(GameData.playerData.Honor);
        this.mLbPearls.string = GameData.playerData.pearls + "";
    }

    RegisteredEvent() {
        EvtManger.on(EventName.monsterDeath, this, this.InitLabel);
        EvtManger.on(EventName.goldUpdate, this, this.InitLabel);
        EvtManger.on(EventName.addGold, this, this.ShowGoldAdd);
        EvtManger.on(EventName.addHonor, this, this.ShowAddHonor);
        EvtManger.on(EventName.updatePearls, this, this.InitLabel)
        EvtManger.on(EventName.addPearls, this, this.ShowAddPearls)
        EvtManger.on(EventName.mainGuide, this, this.ShowGuide);
    }

    ShowGuide() {
        this.mNdGuide.active = true;
        this.mNdGuide.getComponent(Animation).play();
    }

    OnCloseGuide() {
        if (this.mBIsGuideLock) return;
        this.mNdGuide.active = false;
        this.mNdGuide.getComponent(Animation).stop();
    }

    ShowAddPearls(num: number) {
        let addNd = this.mLbPearls.node.parent.getChildByName("addCoin");
        addNd.getComponent(Label).string = "+" + num + "";
        GameData.AddPearls(num);
        this.mLbPearls.node.parent.getComponent(Animation).play();
        this.InitLabel();
    }

    ShowGoldAdd(num: number) {
        if (num <= 0) return;
        let addNd = this.mLbGold.node.parent.getChildByName("addCoin");
        addNd.getComponent(Label).string = "+" + num + "";
        this.mLbGold.node.parent.getComponent(Animation).play();
    }

    ShowAddHonor(num: number) {
        let addNd = this.mLbKills.node.parent.getChildByName("addHonor");
        addNd.getComponent(Label).string = "+" + num + "";
        this.mLbKills.node.parent.getComponent(Animation).play();
    }

    protected onDestroy(): void {
        EvtManger.off(EventName.monsterDeath, this, this.InitLabel);
        EvtManger.off(EventName.goldUpdate, this, this.InitLabel);
        EvtManger.off(EventName.addGold, this, this.ShowGoldAdd);
        EvtManger.off(EventName.addHonor, this, this.ShowAddHonor);
    }

    InitMenuItem() {
        let ltTemp = ["MGAttackLv", "MGCriticalHitRateLv", "MGCriticalHitDamageLv", "MGDamageBoostLv"];
        let ltTempConfig = [DataHelper.GetAttack(), DataHelper.GetCriticalHitRate(), DataHelper.GetCriticalHitDamage(), DataHelper.GetDamageBoost()];
        let lt = this.mContent.children;
        for (let i = 0; i < lt.length; i++) {
            lt[i].addComponent(MenuItem).InitItemByConfig(ltTemp[i], ltTempConfig[i]);
        }
    }

    OnScrollView() {
        AppControll.ShelvedTime = 0;
    }
    start() {
        // [3]
    }
}
